package com.canbot.u05.activity.customvoice.adapter

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.canbot.u05.R
import com.canbot.u05.activity.customvoice.bean.CustomImageData
import com.canbot.u05.glide.GlideApp
import com.canbot.u05.socket.IpMsgConst
import kotlinx.android.synthetic.main.custom_dialog2_pic_item.view.*

/**
 * CustomDialogPicAdapter
 *
 * @author  Liu.Heng
 * @date  2019/5/31
 */
class CustomDialogPicAdapter(private var context: Context, private var list: List<CustomImageData>, var call: Callback) : RecyclerView.Adapter<CustomDialogPicAdapter.MyViewHolder>() {

    private val mCallback = call

    private val layoutInflater: LayoutInflater = LayoutInflater.from(context)

    private var checked = -1


    interface Callback {

        fun clickEvent(v: View, position: Int)
    }

    override fun onCreateViewHolder(p0: ViewGroup, p1: Int): MyViewHolder {
        val inflate = layoutInflater.inflate(R.layout.custom_dialog2_pic_item, p0, false)
        return MyViewHolder(inflate)
    }

    override fun onBindViewHolder(p0: MyViewHolder, position: Int) {
        val picImageView = p0.picImageView
        val picName = p0.picName
        val checkImage = p0.checkImage
        if (checked == position) {
            checkImage.visibility = View.VISIBLE
        } else {
            checkImage.visibility = View.GONE
        }
        val map = list[position]
        val imagePath = map.imagePath
        picName.text = imagePath.replace("/uupic/", "")

        GlideApp.with(context).clear(picImageView)
        /**设置图片圆角角度*/
        val roundedCorners = RoundedCorners(context.resources.getDimension(R.dimen.px30).toInt())
        /**通过RequestOptions扩展功能*/
        val requestOptions = RequestOptions.bitmapTransform(roundedCorners)
        GlideApp.with(context)
                .load(IpMsgConst.HTTP_URL_CHEST + imagePath.replace("/uupic/", "/uupic_thumb/"))
                .thumbnail(0.1f)
                .diskCacheStrategy(DiskCacheStrategy.ALL)/**缓存策略*/
                .skipMemoryCache(false)
                .placeholder(R.drawable.custom_dialog2_pic_bg)/**占位图*/
                .error(R.drawable.custom_dialog2_pic_bg)
                .centerCrop()
                .apply(requestOptions).into(picImageView)
        picImageView.setOnClickListener {
            mCallback.clickEvent(it, position)
        }
    }

    fun setChecked(int: Int) {
        checked = int
        notifyDataSetChanged()
    }

    override fun getItemCount(): Int {
        return list.size
    }

    class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var picImageView: ImageView = view.pic_imageView
        var picName: TextView = view.pic_name
        var checkImage: ImageView = view.pic_selected
    }
}